package com.management.demo.controller.mall;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.demo.common.dto.PageDTO;
import com.management.demo.common.vo.PageVO;
import com.management.demo.domain.mall.PmsAttrGroup;
import com.management.demo.domain.mall.PmsCategory;
import com.management.demo.dto.mall.PmsAttrGroupDTO;
import com.management.demo.dto.mall.PmsAttrGroupQueryDTO;
import com.management.demo.service.PmsAttrGroupService;
import com.management.demo.service.PmsCategoryService;
import com.management.demo.vo.mall.PmsAttrGroupVO;
import com.management.demo.vo.mall.PmsBrandVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "属性分组管理")
@RestController
@RequestMapping("/pms/attr/group")
public class PmsAttrGroupController {

    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;

    @Autowired
    private PmsCategoryService pmsCategoryService;

    @Operation(summary = "分页查询属性分组信息")
    @PostMapping("page")
    public SaResult page(@RequestBody PmsAttrGroupQueryDTO pmsAttrGroupQueryDTO) {
        String categoryId = pmsAttrGroupQueryDTO.getCategoryId();
        String name = pmsAttrGroupQueryDTO.getName();
        Integer currentPage = pmsAttrGroupQueryDTO.getCurrentPage();
        Integer pageSize = pmsAttrGroupQueryDTO.getPageSize();

        LambdaQueryWrapper<PmsAttrGroup> queryWrapper = new LambdaQueryWrapper<PmsAttrGroup>()
                .eq(StringUtils.isNotBlank(categoryId), PmsAttrGroup::getCategoryId, categoryId)
                .like(StringUtils.isNotBlank(name), PmsAttrGroup::getName, name);
        Page<PmsAttrGroup> pmsAttrGroupPage = pmsAttrGroupService.page(new Page<>(currentPage, pageSize), queryWrapper);

        // 查询分类名称
        PageVO<PmsAttrGroupVO> pmsAttrGroupPageVO = PageDTO.getPageVO(pmsAttrGroupPage, item -> {
            PmsAttrGroupVO pmsAttrGroupVO = BeanUtil.copyProperties(item, PmsAttrGroupVO.class);
            String categoryName = pmsCategoryService.getById(pmsAttrGroupVO.getCategoryId()).getName();
            pmsAttrGroupVO.setCategoryName(categoryName);
            return pmsAttrGroupVO;
        });
        return SaResult.ok().setData(pmsAttrGroupPageVO);
    }

    @Operation(summary = "查询所有")
    @GetMapping("list")
    public SaResult list() {
        List<PmsAttrGroup> pmsAttrGroupList = pmsAttrGroupService.list();
        return SaResult.ok().setData(BeanUtil.copyToList(pmsAttrGroupList, PmsAttrGroupVO.class));
    }

    @Operation(summary = "新增/修改属性分组信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody PmsAttrGroupDTO pmsAttrGroupDTO) {
        PmsAttrGroup pmsAttrGroup = BeanUtil.copyProperties(pmsAttrGroupDTO, PmsAttrGroup.class);
        boolean result = pmsAttrGroupService.saveOrUpdate(pmsAttrGroup);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "根据id获取属性分组信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "商品分类id")
                            @PathVariable Integer id) {
        PmsAttrGroup pmsAttrGroup = pmsAttrGroupService.getById(id);
        PmsAttrGroupVO result = BeanUtil.copyProperties(pmsAttrGroup, PmsAttrGroupVO.class);
        result.setCategoryArray(pmsCategoryService.findCategoryPath(result.getCategoryId()));
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "删除属性分组信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "属性分组id")
                           @PathVariable Integer id) {
        boolean result = pmsAttrGroupService.removeById(id);
        return SaResult.ok().setData(result);
    }
}





















